﻿<?php
/**
 * DESCRIPTION:
 *  basic text field, extends Field class
 * FIELDS:
 *  public $size is a size of text field
 *  public $maxlength is a max length of field
 * METHODS:
 *  void __construct($name, $caption, $is_required = false, $value = "", $maxlength = 255, $size = 41, $parameters = "", $help = "", $help_url = "") is a class constructor
 *  string check() validates the field
 *  array get_html() returns html code of field
 * ======
 * @author at8eqeq3
 * @version 0.1
 * @uses utils/strconv.php
 */
class field_text extends field {
    public $size;
    public $maxlength;

    function __construct($name, $caption, $is_required = false, $value = "", $maxlength = 255, $size = 41, $parameters = "", $help = "", $help_url = ""){
        parent::__construct($name, 'text', $caption, $is_required, $value, $parameters, $help, $help_url);

        $this->size = $size;
        $this->maxlength = $maxlength;
    }

    function check(){
        if (!get_magic_quotes_gpc()){
            $this->value = mysql_escape_string($this->value);
        }

        if($this->is_required){
            if(empty($this->value)){
                return "Поле \"".$this->caption."\" не заполнено";
            }
        }
        return "";
    }

    function get_html(){
        if(!empty($this->css_class)){
            $class = "class=\"".$this->css_class."\"";
        }else{
            $class = "";
        }

        if(!empty($this->size)){
            $size = "size=".$this->size;
        }else{
            $size = "";
        }
        if(!empty($this->maxlength)){
            $maxlength = "maxlength=".$this->maxlength;
        }else{
            $maxlength = "";
        }

        $tag = "<input $class type=\"$this->type\" name=\"$this->name\" value=\"".htmlspecialchars($this->value, ENT_QUOTES)."\" $size $maxlength>\n";

        /*if($this->is_required){
            $c = $this->caption;
            $this->caption = "<span class=\"required\">$c</span>";
        }*/

        $help = "";
        if(!empty($this->help)){
            $help .= "<span class=\"form_help\">".nl2br($this->help)."</span>";
        }
        if(!empty($help)){
            $help .= "<br>";
        }
        if(!empty($this->help_url)){
            $help .= "<span class=\"form_help\"><a href=".$this->help_url.">помощь</a></span>";
        }

        return array($this->caption, $tag, $help);
    }
}
?>
